
<template>
  <div>
  <div><h2>登录该网站的用户信息</h2></div>
    <div class="table">
      <el-table
          :data="tableData"
          style="width: 100%">
        <el-table-column
            label="用户名"
            width="180">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>姓名: {{ scope.row.username }}</p>
              <p>住址: {{ scope.row.password }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.username }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
            label="密码"
            width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.password }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  <div class="dialog">
    <el-dialog title="修改信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth" style="width: 400px">
          <el-input v-model="form.username" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" style="width: 400px">
          <el-input v-model="form.password" autocomplete="off" placeholder="请输入新密码" show-password></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmitInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
  </div>
</template>
<script>
import {deleteUser, fetchAllUser, updateUser} from "@/api/user";

export default {
  name: "User",
  data() {
    return {
      centerDialogVisible: false,
      flag:false,
      tableData: [{
        id: '2016-05-02',
        username: '苏超',
        password: '无'
      }, {
        date: '2016-05-04',
        name: '苏超超',
        password: '无'
      }],
      dialogFormVisible: false, //编辑弹窗
      // 弹窗信息
      form: {
        id: '',
        username: '',
        password: ''
      },
      formLabelWidth: '100px'
    }
  },
  created() {
    this.getAllUser()
  },
  methods:{
    getAllUser(){
      fetchAllUser().then((res) => {
        res = res.data
        this.flag = true
        console.log(res)
        this.tableData = res.tableData
      }).catch(() => {
        alert('接口请求失败');
        this.flag = true
      })
    },
    handleEdit(index, row) {
      this.form.username = row.username;
      this.form.password = row.password;
      this.form.id = row.id;
      console.log(index, row);
      this.dialogFormVisible = true;
    },
    handleDelete(index, row) {
      deleteUser(row.id).then(() => {
        this.$alert('删除成功', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.$message({
              type: 'info',
              message: `action: ${ action }`
            });
          }
        });
        this.getAllUser();
      }).catch(() => {
        alert('接口请求失败');
        this.flag = true
      })
      console.log(index, row);
    },
    //  提交用户新信息
    onSubmitInfo() {
      updateUser(this.form).then(() => {
        alert('更新密码成功');
        this.dialogFormVisible = false;
      })
    }
  }
}
</script>
<style scoped>
.dialog {
  display: flex;
}
.table {
  margin-left: 500px;
  width: 50%;
  display: flex;

}
</style>
